package emulator.hardware.nmos6502.operands;

import emulator.assembler.SymbolTable;
import emulator.hardware.HwWord;
import emulator.hardware.debug.BusWatchException;
import emulator.hardware.memory.UnmappedMemoryException;
import emulator.hardware.nmos6502.Cpu6502;
import emulator.support.IndirectAddressOperandEval;
import emulator.support.OperandEval;
import emulator.util.MemoryPtr;

public class IndirectAddress extends ImmediateAddress {
	private int pointer;

	@Override
	public void init(Cpu6502 cpu) throws UnmappedMemoryException, BusWatchException {
		pointer = cpu.fetchProgramWord();
		if ((pointer & 0xff) == 0xff)
		{	// JMP on page border
			addr = new HwWord(cpu.readByte(pointer)
							  + (cpu.readByte(pointer-0xff) << 8));
		}
		else
			addr = new HwWord(cpu.readWord(pointer));
	}

	/* (non-Javadoc)
	 * @see emulator.hardware.nmos6502.operands.ImmediateAddress#getString()
	 */
	@Override
	public String getString() {
		return "($"+new HwWord(pointer)+")   ; $"+addr;
	}

	/* (non-Javadoc)
	 * @see emulator.hardware.nmos6502.operands.ImmediateAddress#getString(emulator.util.MemoryPtr, emulator.assembler.SymbolTable)
	 */
	@Override
	public String getString(MemoryPtr mem_ref, SymbolTable symbols) {
		return "("+symbols.getLabelOrAddress(mem_ref.getWordOp())+")";
	}

	@Override
	public OperandEval getEvaluator(MemoryPtr mem_ref) {
		return new IndirectAddressOperandEval(mem_ref);
	}
}
